<template lang="html">
    <div class="guideedit">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item><i class="el-icon-message"></i>&nbsp;修改网站操作指南</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <el-form ref="form" label-width="120px">
                <el-form-item label="标题：">
                    <el-input v-model="name" placeholder="请输入标题" :disabled="true" style="width:360px"></el-input>
                </el-form-item>
                <el-form-item label="内容(英文)：">
                    <div class="news-editor">
                        <ueditor ref="contentEn" @load="editorLoadEN" :width="editor_style.width" :height="editor_style.height" id="editor"></ueditor>
                    </div>
                    <div style="color:red;padding-left:10px;">*请输入英文内容 </div>
                </el-form-item>
                
                <el-form-item>
                    <el-button style="width:150px" type="primary" @click="submit()">确认修改</el-button>
                    <router-link to="/guide"><el-button style="width:150px" type="primary">返回列表</el-button></router-link>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
import {getGuideList,postGuideEdit} from '@/api/guide/list'
// import editor from '../../components/common/editor.vue'
import ueditor from '../../components/common/ueditor.vue'
    export default {
        name: "guideedit",
        components:{
            // editor,
            ueditor,
        },
        data() {
            return {
                id:0,//列表id
                name:'',//标题
                contentCn:'',//中文内容
                contentEn:'',//英文内容
                editor_style: {
                    width: 'auto',
                    height: 'auto'
                },
            }
        },
        created(){
            //获取id
            if(this.$route.query.id || this.$route.query.id != ''){
                this.id = this.$route.query.id;
            }
            //获取数据
           this.getGuideList();
        },
        mounted() {
            
        },
        methods: {
            //获取列表
            getGuideList(){
                var that = this;
                getGuideList().then(res => {
                    // console.log(res);
                    var list = res.data;
                    list.forEach(function(val,index){
                        if(that.id == val.id){
                            that.name = val.name;
                            that.contentCn = val.content_cn;
                            that.contentEn = val.content_en;
                            //把获取的内容显示在富文本编辑器里
                            that.$refs.contentEn.setHtml(that.contentEn);
                        }
                    })
                })
            },
            //确认提交修改  
            submit(){
                //获取富文本编辑器里的内容
                this.contentEn = this.$refs.contentEn.getHtml();
                if(this.name == ''){
                    this.$message({
                        message: '请输入标题！',
                        type: 'warning'
                    });
                    return false;
                }
                if(this.contentEn == ''){
                     this.$message({
                        message: '请输入英文内容！',
                        type: 'warning'
                    });
                    return false;
                }
                //保存修改
                postGuideEdit(this.id,this.contentCn,this.contentEn).then(res => {
                     let _this = this;
                    if(res.code == 200){
                        this.$message({
                            message: '修改成功！',
                            type: 'success'
                        });
                        setTimeout(function(){
                            _this.$router.push({ path: '/guide',query:{}});
                        },2100)
                    }else {
                        this.$message.error('修改失败！');
                    }
                })
            },
            editorLoadEN(originalName, resultText) {
                // originalName 上传文件名，resultText 服务器端返回的text
                this.$refs.contentEn.insertImg(resultText)
            },
        }
    }
</script>

<style lang="css" scoped>

</style>
